package com.jiemengshi.server.controller;

import com.jiemengshi.server.model.UserAdmin;
import com.jiemengshi.server.service.AdminUserService;
import com.jiemengshi.server.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by MingQi on 2017/10/14.
 */
@Controller
@RequestMapping(value = {"/admin/user"}, produces = {"application/json;charset=UTF-8"})
public class AdminUserController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private AdminUserService userService;

    @ResponseBody
    @RequestMapping("/add")
    public String addUserAdmin(HttpServletRequest request, UserAdmin admin){
        try {
            if(userService.getLoginUserPermissionLevel(request) != 0)
                throw new PrjException(ErrorCodeMsg.USER_ADMIN_NO_AUTHORITY);
            
            if(StringUtil.isEmpty(admin.getUsername()) || StringUtil.isEmpty(admin.getPassword()) || admin.getPermissionLevel() == null || admin.getPermissionLevel() < 0 || admin.getPermissionLevel() > 1)
                throw new PrjException(ErrorCodeMsg.PARAMETER_ERROR);

            if(userService.addUserAdmin(admin))
                return ApiResult.SUCCESS;
            else
                throw new PrjException(ErrorCodeMsg.OPERATION_FAIL);
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("/delete")
    public String deleteUserAdmin(HttpServletRequest request, String username){
        try {
            if(userService.getLoginUserPermissionLevel(request) != 0)
                throw new PrjException(ErrorCodeMsg.USER_ADMIN_NO_AUTHORITY);

            if(StringUtil.isEmpty(username))
                throw new PrjException(ErrorCodeMsg.PARAMETER_ERROR);

            if(userService.deleteUserAdminByUsername(username))
                return ApiResult.SUCCESS;
            else
                throw new PrjException(ErrorCodeMsg.OPERATION_FAIL);
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }
    
    @ResponseBody
    @RequestMapping("/login")
    public String userLogin(HttpServletRequest request, UserAdmin admin){
        try {
            if(StringUtil.isEmpty(admin.getUsername()) || StringUtil.isEmpty(admin.getPassword()))
                throw new PrjException(ErrorCodeMsg.PARAMETER_ERROR);

            admin = userService.userAdminLogin(request, admin.getUsername(), admin.getPassword());
            if (admin != null) {
                admin.setPassword(null);
                return ApiResult.writeData(admin);
            } else {
                throw new PrjException(ErrorCodeMsg.USER_ADMIN_LOGIN_FAIL);
            }
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("/logout")
    public String userLogout(HttpServletRequest request){
        try {
            if(userService.userAdminLogout(request))
                return ApiResult.SUCCESS;
            else
                throw new PrjException(ErrorCodeMsg.OPERATION_FAIL);
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("/wechat/level/update")
    public String updateUserWechatLevel(HttpServletRequest request, int userId, int level){
        try {
            if(userId < 1 || level < 0)
                throw new PrjException(ErrorCodeMsg.PARAMETER_ERROR);
            
            if(userService.getLoginUserPermissionLevel(request) != 0)
                throw new PrjException(ErrorCodeMsg.USER_ADMIN_NO_AUTHORITY);
            
            if(userService.updateUserWechatLevel(userId, level))
                return ApiResult.SUCCESS;
            else
                throw new PrjException(ErrorCodeMsg.OPERATION_FAIL);
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }
    
    @ResponseBody
    @RequestMapping("/login/session")
    public String userLoginSession(HttpServletRequest request){
        try {
            request.getSession().setAttribute("adminInfo", "1_0");

            return ApiResult.SUCCESS;
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }
}
